<template>
	<view class="balance">
		<label class="radio">
			<radio :checked="isCheckedAll" color="#E4393C" @click="onCheckedAllClick" /><text>全选</text>
		</label>
		<view class="total">
			合计: <text class="total-price">￥{{ totalPrice.toFixed(2) }}</text>
		</view>
		<view class="confirm-balance" @click="onBalance">
			结算
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapGetters,
		mapMutations
	} from 'vuex'
	export default {
		name: "Balance",
		data() {
			return {
				seconds: 3,
				timer: null
			};
		},
		methods: {
			...mapMutations('cart', ['updateAllGoodsState']),
			onCheckedAllClick() {
				this.updateAllGoodsState(!this.isCheckedAll)
			},
			onBalance() {
				if (!this.hasBeenChecked) {
					return uni.$showMsg('请选择商品')
				}

				if (!this.detailAddress) {
					return uni.$showMsg('请选择收货地址')
				}

				if (!this.token) {
					return this.delayNavigate()
				}
			},
			delayNavigate() {
				this.seconds = 3
				this.showTips(this.seconds)
				this.timer = setInterval(() => {
					this.seconds--
					if (this.seconds <= 0) {
						clearInterval(this.timer)
						uni.switchTab({
							url: '/pages/profile/profile'
						})
						return 
					}
					this.showTips(this.seconds)
				}, 1000)
			},
			showTips(n) {
				uni.showToast({
					icon: 'none',
					title: '请登录后再结算!' + n + '秒后自动跳转到登录页',
					mask: true,
					duration: 1500
				})
			}
		},
		computed: {
			...mapState('user', ['token']),
			...mapGetters('cart', ['isCheckedAll', 'totalPrice', 'hasBeenChecked']),
			...mapGetters('user', ['detailAddress'])
		},
	}
</script>

<style lang="scss">
	.balance {
		position: fixed;
		bottom: 1px;
		width: 100%;
		display: flex;
		align-items: center;
		height: 96rpx;
		text-align: center;
		line-height: 96rpx;
		font-size: 28rpx;
		border-top: 1px solid #f0f2f5;
		z-index: 9999;
		background-color: #fff;

		.radio {
			height: 100%;
			flex: 1
		}

		.total {
			height: 100%;
			flex: 2;

			.total-price {
				font-weight: 700;
				color: #E4393C;
			}
		}

		.confirm-balance {
			flex: 1;
			height: 100%;
			background-color: #E4393C;
			color: #fff;
			font-size: 32rpx;
		}
	}
</style>
